#include <iostream>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
    int pipefd[2] = {0, 0};
    char buffer[1024];
    if (pipe(pipefd) == -1)
    {
        std::cout << "pipe failed!" << std::endl;
        exit(-1);
    }
    pid_t id = fork();
    if (id == 0)
    {
        // 子进程
        // 关闭写端
        close(pipefd[1]);
        read(pipefd[0], buffer, 1024);
        std::cout << "buffer: " << buffer << std::endl;
        close(pipefd[0]);
        exit(-1);
    }
    // 父进程
    // 关闭读端
    close(pipefd[0]);
    const char *str = "i am father";
    write(pipefd[1], str, strlen(str) + 1);
    close(pipefd[1]);
    wait(NULL);
    exit(-1);
    return 0;
}